/**
 * Copyright(c) 2013-2015 by Puhuifinance Inc.
 * All Rights Reserved
 */
package com.puhui.chome.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.puhui.chome.controller.base.BaseController;
import com.puhui.chome.model.Template;
import com.puhui.chome.service.TemplateService;
import com.puhui.chome.util.StringUtil;

/**
 * @author dingxingang
 */
@Controller
@RequestMapping("/template")
public class TemplateController extends BaseController {

    @Autowired
    private TemplateService templateService;

    /**
     * 跳转列表页面
     * 
     * @author dingxingang
     * @return 跳转地址
     */
    @RequestMapping(value = "/toList")
    public String toList() {
        return "template/templateList";
    }

    /**
     * 列表
     * 
     * @author dingxingang
     * @param templateVo
     *            查询参数
     * @param page
     *            页码
     * @param size
     *            每页显示条数
     * @param sort
     *            排序
     * @param order
     *            正序or倒序
     * @return 列表
     */
    @ResponseBody
    @RequestMapping(value = "/templateList", method = RequestMethod.POST)
    public Object templateList(Template templateVo, @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "rows", defaultValue = "20") int size,
            @RequestParam(value = "sort", defaultValue = "id") String sort,
            @RequestParam(value = "order", defaultValue = "desc") String order) {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        param.put("template", templateVo);
        // 查询总数
        long total = templateService.findTemplateCount(param);
        int from = (page - 1) * size; // 计算开始条数
        // 查询数据
        param.put("from", from);
        param.put("size", size);
        param.put("sort", StringUtil.getFieldName(sort));
        param.put("order", order);
        List<Template> list = new ArrayList<>();
        if (total > 0) {
            list = templateService.findTemplate(param);
        }
        // 返回信息
        Map<String, Object> rev = new HashMap<>();
        rev.put("total", total);
        rev.put("rows", list);
        return rev;
    }

    /**
     * 跳转详细页面
     * 
     * @author dingxingang
     * @param model
     *            消息
     * @param id
     *            主键
     * @return 跳转地址
     */
    @RequestMapping(value = "/toView")
    public String toView(ModelMap model, Long id) {
        if (id != null) {
            Template template = templateService.getTemplateById(id);
            model.put("template", template);
        }
        return "template/templateView";
    }

}
